<?php
function mergeAttrs($attrs=array()) {
	$mergedAttrs = $attrs;
	if(isset($attrs)) {
		foreach ($attrs as $key => $value) {
			if (strtolower($key) == 'class') {
				if($mergedAttrs[$key] != $value)
				$value = $mergedAttrs[$key] . ' ' . $value;
			} else if (strtolower($key) == 'style') {
				if($mergedAttrs[$key] != $value)
				$value = $mergedAttrs[$key] . ';' . $value;
			}
		}
		$mergedAttrs[$key] = $value;
	}
	return $mergedAttrs;
}

function getAttrString($attrs=array()) {
	$attrPairs = array();
	if(isset($attrs)) {
		foreach ($attrs as $name => $value) {
			$value = htmlentities($value, ENT_QUOTES);
			$attrPairs[] = "$name=\"$value\"";
		}
	}
	return join(' ', $attrPairs);
}

function anchor($href,$label,$attrs=array(),$selected=false) {
	$id = generateId($href);
	$url = 'http://'.HOST."/$href";
	if($selected == true) {
		if($href == URL) {
			if(isset($attrs['class'])) {
				$attrs['class'] = $attrs['class'].' selected';
			} else {
				$attrs['class'] = 'selected';
			}
		}
	}
	$attr_string = getAttrString($attrs);
	$body = "<a $attr_string href='$url'>$label</a></li>";
	return $body;
}

function newText($name,$label,$attrs=array(),$stack=true) {
	$id = generateId($name);
	$type = $attrs['type']? $attrs['type'] : 'text';
	if(isset($attrs['description'])) { $description = $attrs['description']; unset($attrs['description']); }
	if(isset($attrs['class'])) { $div['class'] = $attrs['class']; unset($attrs['class']); }
	if(isset($attrs['style'])) { $div['style'] = $attrs['style']; unset($attrs['style']); }
	$divString = getAttrString(mergeAttrs($div));
	$attrString = getAttrString($attrs);
	$labelStr = $label === ''? '' : "<label id='lbl-$id' for='$id'>$label: </label>";
	$body =	"$labelStr
			<input id='$id' name='$name' type='$type' $attrString>
			<span id='$id-validate' class='invisible hover'></span>
			$description";
			if($stack===true) {
				return "<div id='row-$id' $divString>$body</div>";
			} else {
				return $body;
			}
}

function newTextArea($name,$label,$attrs=array(),$stack=true) {
	$id = generateId($name);
	if(isset($attrs['description'])) { $description = $attrs['description']; unset($attrs['description']); }
	if(isset($attrs['class'])) { $div['class'] = $attrs['class']; unset($attrs['class']); }
	if(isset($attrs['style'])) { $div['style'] = $attrs['style']; unset($attrs['style']); }
	$divString = getAttrString(mergeAttrs($div));
	$attrString = getAttrString($attrs);
	$labelStr = $label === ''? '' : "<label id='lbl-$id' for='$id' style='vertical-align:top;'>$label: </label>";
	$body =	"$labelStr
			<textarea id='$id' name='$name' $attrString></textarea>
			<span id='$id-validate' class='invisible hover'></span>
			$description";
			if($stack===true) return "<div id='row-$id' $divString>$body</div>";
			else return $body;
}

function newFullName($label,$attrs=array()) {
	$body = newText("first-name",$label,array('value'=>'first name'),false);
	//$body .= newText("initial-$name",'',array('maxlength'=>'5','size'=>'1','value'=>'initial'),false);
	$body .= newText("last-name",'',array('value'=>'last name'),false);

	return "<div id='row-full-name'>$body</div>";
}

function newFileUpload($name,$attrs=array()) {
	$id = generateId($name);
	$body = "<input name='$name' type='file' id='$id'>";
	return $body;
}

function newBtn($name,$label,$a=array(),$stack=true) {
	$id = generateId($name);
	$type = $a['type']? $a['type'] : "submit";
	if($type == 'submit') $a['disabled'] = true;
	if($a['disabled'] == true) { $disabled = 'disabled="disabled"'; unset($a['disabled']); } 
	$class = $a['class']? "class='{$a['class']}'" : "";
	$onclick = $a['onclick']? "onclick='{$a['onclick']}'" : "";

	$body = "<input id='$id' name='$name' type='$type' $disabled value='$label' $onclick>";
	
	if($stack == true)
		return "<div id='row-$id-btn' $class>$body</div>";
	else return $body;
}

function imageAnchor($href,$id,$image,$width,$height,$class_name='') {
	$onmouseover = $class_name? "this.className=&#39img-btn-hover $class_name&#39" : "this.className=&#39img-btn-hover&#39";
	$onmouseout = $class_name? "this.className=&#39img-btn $class_name&#39" : "this.className=&#39img-btn&#39";
	$onmousedown = $class_name? "this.className=&#39img-btn-down $class_name&#39" : "this.className=&#39img-btn-down&#39";
	$class = $class_name? "class='$class_name'" : "";
	return "
	<div id='$id' $class>
	<a href='$href' class='img-btn'
	style='background:url(/includes/images/btn/$image) no-repeat scroll left top;width:{$width}px;height:{$height}px'
	onmouseover='$onmouseover' onmouseout='$onmouseout' onmousedown='$onmousedown'>&nbsp;</a>
	</div>";
}

function newTabs($data,$width) {
	$count = 0;
	$body = "<div id='tabs-bar'>";
	foreach($data as $key=>$value) {
		if($value) {
			$count++;
			$id = generateId($key);
			$pClass = $count === 1? 'selected' : '';
			$divClass = $count === 1? 'tab selected' : 'tab';
			$body .= "<div id='tab-$id' class='$divClass'>
				  <p id='$id'
				  onclick='javascript:showTab(&#39;$id&#39;,&#39;$count&#39;)'
				  class='$pClass'>$key</p></div>";
		}
	}
	$body .= "</div><div id='tabs-content'>";
	$count = 0;
	foreach($data as $key=>$value) {
		if($value) {
			$count++;
			$id = generateId($key);
			$class = $count === 1? 'visible' : 'invisible';
			$body .= "<div id='content-$id' style='width:$width;' class='$class'>$value</div>";
		}
	}
	$body .= "</div><div class='clear'></div>";
	return $body;
}

function popup($target,$content,$width,$pos = array(),$closer = false) {
	if(isset($pos['left'])) $l = 'left:'.$pos['left'].'px;'; else $l = '';
	if(isset($pos['right'])) $r = 'right:'.$pos['right'].'px;'; else $r = '';
	if(isset($pos['bottom'])) $b = 'bottom:'.$pos['bottom'].'px;'; else $b = '';
	if(isset($pos['top'])) $t = 'top:'.$pos['top'].'px;'; else $t = '';
	$id = generateId($target);
	if($closer == true)
		$close = '<div class="close-popup">'.imageAnchor('javascript:close_popup();',"close_popup_$id",'close-popup.png',25,25).'</div>';
	print "<div id='popup-$id' style='display:none;width:{$width}px;{$l}{$r}{$b}{$t}' class='popup'>
		$close
		<div class='popup-content'>$content</div>
		</div>";
}
?>